package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ArticleDetail;
import com.ruoyi.system.domain.AuditArticle;
import com.ruoyi.system.mapper.ArticleDetailMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ArticleMapper;
import com.ruoyi.system.domain.Article;
import com.ruoyi.system.service.IArticleService;

/**
 * 文章管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-01
 */
@Service
public class ArticleServiceImpl implements IArticleService 
{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleDetailMapper articleDetailMapper;

    /**
     * 查询文章管理
     * 
     * @param id 文章管理主键
     * @return 文章管理
     */
    @Override
    public Article selectArticleById(Long id)
    {
        return articleMapper.selectArticleById(id);
    }

    /**
     * 查询文章管理列表
     * 
     * @param article 文章管理
     * @return 文章管理
     */
    @Override
    public List<Article> selectArticleList(Article article)
    {
        return articleMapper.selectArticleList(article);
    }

    /**
     * 新增文章管理
     * 
     * @param article 文章管理
     * @return 结果
     */
    @Override
    public int insertArticle(Article article)
    {
        article.setCreateTime(DateUtils.getNowDate());
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章管理
     * 
     * @param article 文章管理
     * @return 结果
     */
    @Override
    public int updateArticle(Article article)
    {
        article.setUpdateTime(DateUtils.getNowDate());
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章管理
     * 
     * @param ids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(Long[] ids)
    {
        return articleMapper.deleteArticleByIds(ids);
    }

    /**
     * 删除文章管理信息
     * 
     * @param id 文章管理主键
     * @return 结果
     */
    @Override
    public int deleteArticleById(Long id)
    {
        return articleMapper.deleteArticleById(id);
    }

    @Override
    public AuditArticle selectAuditArticleById(Long id) {
        Article article = articleMapper.selectArticleById(id);
        ArticleDetail articleDetail = articleDetailMapper.selectArticleDetailByArticleId(id);

        if(article != null && articleDetail != null){
            AuditArticle auditArticle = new AuditArticle();
            BeanUtils.copyProperties(article,auditArticle);
            BeanUtils.copyProperties(articleDetail,auditArticle);

            // 检查 articlePhoto 是否为 null
            String articlePhoto = auditArticle.getArticlePhoto();
            if (articlePhoto != null) {
                // 处理 articlePhoto 的路径
                int startIndex = articlePhoto.indexOf("/") + 1;
                if (startIndex != 0) {
                    String relativePath = articlePhoto.substring(startIndex);
                    relativePath = relativePath.substring(relativePath.indexOf("/")+1);
                    auditArticle.setArticlePhoto("http://localhost:10002/" + relativePath);
                }
            }
            return auditArticle;
        }
        return null;
    }

    @Override
    public int changeAuditState(Long id) {
        return articleMapper.updateAuditStatus(id);
    }
}
